<template>
  <div>
    <el-card style="margin: 20px 0">
      <CategorySelect @getCategoryId="getCategoryId" :show="showTable" />
    </el-card>
    <el-card>
      <div v-show="showTable">
        <el-button
          type="primary"
          icon="el-icon-plus"
          :disabled="!category3Id"
          @click="addAttr"
          >添加属性</el-button
        >
        <el-table style="width: 100%" border :data="attrList">
          <el-table-column
            type="index"
            label="序号"
            width="80px"
            align="center"
          >
          </el-table-column>
          <el-table-column prop="attrName" label="属性名称" width="150px">
          </el-table-column>
          <el-table-column prop="prop" label="属性值列表" width="width">
            <template slot-scope="{ row, $index }">
              <el-tag
                type="success"
                style="margin: 0 20px"
                v-for="(attrValue, index) in row.attrValueList"
                :key="attrValue.id"
                >{{ attrValue.valueName }}</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="150px">
            <template slot-scope="{ row, $index }">
              <el-button
                type="warning"
                icon="el-icon-edit"
                size="mini"
                @click="updateAttr(row)"
              ></el-button>
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 添加|修改属性的结构 -->
      <div v-show="!showTable">
        <el-form :inline="true" ref="form" label-width="80px" :model="attrInfo">
          <el-form-item label="属性名">
            <el-input
              placeholder="请输入属性名"
              v-model="attrInfo.attrName"
            ></el-input>
          </el-form-item>
        </el-form>
        <el-button type="primary" icon="el-icon-plus" @click="addAttrValue"
          >添加属性值</el-button
        >
        <el-table
          style="width: 100%; margin: 20px 0"
          border
          :data="attrInfo.attrValueList"
        >
          <el-table-column align="center" type="index" label="序号" width="80">
          </el-table-column>
          <el-table-column label="属性值名称">
            <template slot-scope="{ row, $index }">
              <el-input
                v-model="row.valueName"
                placeholder="请输入属性值名称"
                size="mini"
                v-show="row.editMode"
                @blur="toLookMode(row, $index)"
                @keyup.native.enter="toLookMode(row, $index)"
                :ref="$index"
              ></el-input>
              <span
                v-show="!row.editMode"
                @click="toEdit(row, $index)"
                style="display: block"
                >{{ row.valueName }}</span
              >
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="{ row, $index }">
              <el-popconfirm
                :title="`确定删除${row.valueName}吗？`"
                @onConfirm="deleteAttrValue($index)"
              >
                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  size="mini"
                  slot="reference"
                ></el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-button type="primary" @click="addOrUpdateAttr">保存</el-button>
        <el-button @click="showTable = true">取消</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import cloneDeep from "lodash/cloneDeep";
export default {
  name: "Attr",
  data() {
    return {
      category1Id: "",
      category2Id: "",
      category3Id: "",
      // 平台属性
      attrList: [],
      // 控制table显示与隐藏
      showTable: true,
      // 添加或修改要收集的属性
      attrInfo: {
        attrName: "",
        attrValueList: [],
        categoryId: 0,
        categoryLevel: 3,
      },
    };
  },
  methods: {
    // 自定义事件的回调
    getCategoryId(obj) {
      Object.assign(this, obj);
      if (!this.category3Id) return (this.attrList = []);
      this.getAttrList();
    },
    // 获取品牌属性的数据
    async getAttrList() {
      let result = await this.$API.attr.reqAttrList(
        this.category1Id,
        this.category2Id,
        this.category3Id
      );
      if (result.code === 200) {
        this.attrList = result.data;
      }
    },
    // 点击添加属性
    addAttr() {
      this.attrInfo = {
        attrName: "",
        attrValueList: [],
        categoryId: 0,
        categoryLevel: 3,
      };
      this.attrInfo.categoryId = this.category3Id;
      this.showTable = false;
    },
    // 修改属性
    updateAttr(row) {
      this.attrInfo = cloneDeep(row);
      this.showTable = false;

      this.attrInfo.attrValueList.forEach((v) => {
        this.$set(v, "editMode", false);
      });
    },
    // 点击添加属性值
    addAttrValue() {
      this.attrInfo.attrValueList.push({
        attrId: this.attrInfo.id,
        valueName: "",
        editMode: true,
      });
      this.$nextTick(() => {
        this.$refs[this.attrInfo.attrValueList.length - 1].focus();
      });
    },
    // 失焦或者按下回车，转成查看模式
    toLookMode(row, $index) {
      row.valueName = row.valueName.trim();
      if (row.valueName === "") {
        this.attrInfo.attrValueList.splice($index, 1);
        this.$message("属性值不能为空");
        return;
      }
      const isRepeat = this.attrInfo.attrValueList.some((v) => {
        return v === row ? false : v.valueName === row.valueName;
      });
      if (isRepeat) {
        this.attrInfo.attrValueList.splice($index, 1);
        this.$message("属性值不能重复");
        return;
      }
      row.editMode = false;
    },
    // 点击span，转成编辑模式
    toEdit(row, $index) {
      row.editMode = true;
      this.$nextTick(() => {
        this.$refs[$index].focus();
      });
    },
    // 确定删除属性值
    deleteAttrValue($index) {
      this.attrInfo.attrValueList.splice($index, 1);
    },
    // 点击保存
    async addOrUpdateAttr() {
      this.attrInfo.attrName = this.attrInfo.attrName.trim();
      if (!this.attrInfo.attrName)
        return this.$message.warning("属性名不能为空");
      try {
        this.showTable = true;
        await this.$API.attr.reqAddOrUpdateAttr(this.attrInfo);
        this.$message.success("保存成功");
        this.getAttrList();
      } catch (e) {}
    },
  },
};
</script>

<style></style>
